2 Copyright (C) 2024 Rubén Beltrán del Río
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see https://map.tranquil.systems.
20 class MapTextEditorController: NSViewController {
22 @Binding var document: MapDocument
23 var highlightRanges: [Range<String.Index>] {
29 var selectedRange: Int {
36 let onChange: () -> Void
38 private let vertexRegex = MapParsingPatterns.vertex
39 private let edgeRegex = MapParsingPatterns.edge
40 private let blockerRegex = MapParsingPatterns.blocker
41 private let opportunityRegex = MapParsingPatterns.opportunity
42 private let noteRegex = MapParsingPatterns.note
43 private let stageRegex = MapParsingPatterns.stage
44 private let groupRegex = MapParsingPatterns.group
46 private let changeDebouncer: Debouncer = Debouncer(seconds: 1)
49 document: Binding<MapDocument>, highlightRanges: [Range<String.Index>], selectedRange: Int,
50 onChange: @escaping () -> Void
52 self._document = document
53 self.onChange = onChange
54 self.highlightRanges = highlightRanges
55 self.selectedRange = selectedRange
56 super.init(nibName: nil, bundle: nil)
59 required init?(coder: NSCoder) {
60 fatalError("init(coder:) has not been implemented")
63 override func loadView() {
64 let scrollView = NSTextView.scrollableTextView()
65 let textView = scrollView.documentView as! NSTextView
67 scrollView.translatesAutoresizingMaskIntoConstraints = false
69 textView.backgroundColor = .ui.background
70 textView.allowsUndo = true
71 textView.delegate = self
72 textView.textStorage?.delegate = self
73 textView.string = self.document.text
74 textView.isEditable = true
75 textView.font = .monospacedSystemFont(ofSize: 16.0, weight: .regular)
76 self.view = scrollView
79 override func viewDidAppear() {
80 self.view.window?.makeFirstResponder(self.view)
84 private var textView: NSTextView? {
85 return (view as? NSScrollView)?.documentView as? NSTextView
88 private func updateHighlights() {
90 if let textStorage = textView.textStorage {
91 textStorage.removeAttribute(
92 .backgroundColor, range: NSRange(location: 0, length: textStorage.length))
94 for range in highlightRanges {
95 let nsRange = NSRange(range, in: textStorage.string)
97 textStorage.addAttribute(.backgroundColor, value: NSColor.syntax.match, range: nsRange)
100 textView.needsDisplay = true
106 private func focusOnResult() {
108 if let textStorage = textView.textStorage {
109 if selectedRange < highlightRanges.count {
110 let range = highlightRanges[selectedRange]
111 let nsRange = NSRange(range, in: textStorage.string)
112 textView.scrollRangeToVisible(nsRange)
113 textView.selectedRange = nsRange
119 private func setSelectionColor() {
120 guard let textView = self.textView else { return }
122 var selectedTextAttributes = textView.selectedTextAttributes
123 selectedTextAttributes[.backgroundColor] = NSColor.yellow.withAlphaComponent(0.3)
124 textView.selectedTextAttributes = selectedTextAttributes
128 extension MapTextEditorController: NSTextViewDelegate {
130 func textDidChange(_ obj: Notification) {
131 if let textField = obj.object as? NSTextView {
132 self.document.text = textField.string
134 changeDebouncer.debounce {
135 DispatchQueue.main.async {
142 func textView(_ view: NSTextView, shouldChangeTextIn: NSRange, replacementString: String?) -> Bool
144 let range = Range(shouldChangeTextIn, in: view.string)
145 let target = view.string[range!]
155 extension MapTextEditorController: NSTextStorageDelegate {
157 override func textStorageDidProcessEditing(_ obj: Notification) {
158 if let textStorage = obj.object as? NSTextStorage {
159 self.colorizeText(textStorage: textStorage)
163 private func colorizeText(textStorage: NSTextStorage) {
164 let range = NSMakeRange(0, textStorage.length)
165 var matches = vertexRegex.matches(in: textStorage.string, options: [], range: range)
167 for match in matches {
168 textStorage.addAttributes(
169 [.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 1))
170 textStorage.addAttributes(
171 [.foregroundColor: NSColor.syntax.number], range: match.range(at: 2))
172 textStorage.addAttributes(
173 [.foregroundColor: NSColor.syntax.number], range: match.range(at: 3))
174 textStorage.addAttributes(
175 [.foregroundColor: NSColor.syntax.option], range: match.range(at: 4))
178 matches = edgeRegex.matches(in: textStorage.string, options: [], range: range)
180 for match in matches {
181 textStorage.addAttributes(
182 [.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 1))
183 let arrowRange = match.range(at: 2)
184 textStorage.addAttributes(
185 [.foregroundColor: NSColor.syntax.symbol],
186 range: NSMakeRange(arrowRange.lowerBound - 1, arrowRange.length + 1))
187 textStorage.addAttributes(
188 [.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 3))
191 matches = opportunityRegex.matches(in: textStorage.string, options: [], range: range)
193 for match in matches {
194 textStorage.addAttributes(
195 [.foregroundColor: NSColor.syntax.option], range: match.range(at: 1))
196 textStorage.addAttributes(
197 [.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 2))
198 textStorage.addAttributes(
199 [.foregroundColor: NSColor.syntax.symbol], range: match.range(at: 3))
200 textStorage.addAttributes(
201 [.foregroundColor: NSColor.syntax.number], range: match.range(at: 4))
204 matches = blockerRegex.matches(in: textStorage.string, options: [], range: range)
206 for match in matches {
207 textStorage.addAttributes(
208 [.foregroundColor: NSColor.syntax.option], range: match.range(at: 1))
209 textStorage.addAttributes(
210 [.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 2))
213 matches = noteRegex.matches(in: textStorage.string, options: [], range: range)
215 for match in matches {
216 textStorage.addAttributes(
217 [.foregroundColor: NSColor.syntax.option], range: match.range(at: 1))
218 textStorage.addAttributes(
219 [.foregroundColor: NSColor.syntax.number], range: match.range(at: 2))
220 textStorage.addAttributes(
221 [.foregroundColor: NSColor.syntax.number], range: match.range(at: 3))
224 matches = stageRegex.matches(in: textStorage.string, options: [], range: range)
226 for match in matches {
227 textStorage.addAttributes(
228 [.foregroundColor: NSColor.syntax.option], range: match.range(at: 1))
229 textStorage.addAttributes(
230 [.foregroundColor: NSColor.syntax.number], range: match.range(at: 2))
233 matches = groupRegex.matches(in: textStorage.string, options: [], range: range)
235 for match in matches {
236 textStorage.addAttributes(
237 [.foregroundColor: NSColor.syntax.option], range: match.range(at: 1))
238 textStorage.addAttributes(
239 [.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 2))
244 struct MapTextEditor: NSViewControllerRepresentable {
246 @Binding var document: MapDocument
247 var highlightRanges: [Range<String.Index>]
248 var selectedRange: Int
249 var onChange: () -> Void = {}
251 func makeNSViewController(
252 context: NSViewControllerRepresentableContext<MapTextEditor>
253 ) -> MapTextEditorController {
254 return MapTextEditorController(
255 document: $document, highlightRanges: highlightRanges, selectedRange: selectedRange,
259 func updateNSViewController(
260 _ nsViewController: MapTextEditorController,
261 context: NSViewControllerRepresentableContext<MapTextEditor>
263 nsViewController.highlightRanges = highlightRanges
264 nsViewController.selectedRange = selectedRange